package system.wrap_filter;

import java.util.ArrayList;
import models.SubscribeEvents;
import system.eventBuffer.EventBuffer;
import system.events.Event;
import system.events.EventType;
import system.events.SoftwareEvent;
import system.events.HardwareEvent;
import system.main.SystemDataStructure;

public class Wrapper extends Thread {
	Event event;
	Standard standard;
	EventBuffer eventBuffer;
	ArrayList<Policy> policies;
	ArrayList<Policy> H_policies;
	ArrayList<Policy> S_policies;
	SubscribeEvents subscribeEvents;
	SystemDataStructure ds;
	long timeStamp;

	public Wrapper(ArrayList<Policy> policies, SystemDataStructure ds) {
		this.ds = ds;
		this.eventBuffer = ds.getEventBuffer();
		this.policies = policies;
		H_policies = new ArrayList<Policy>();
		S_policies = new ArrayList<Policy>();
		for (int i = 0; i < policies.size(); i++) {
			if (policies.get(i).getSource().equals("hardware")) {
				H_policies.add(policies.get(i));
			} else if (policies.get(i).getSource().equals("software")) {
				S_policies.add(policies.get(i));
			}
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		if ((event = eventBuffer.poll()) != null) {
			timeStamp = System.currentTimeMillis();
			if (event.getType() == EventType.SOFTWARE) {
				standard = new Standard(timeStamp, event.getType(), null,
						null, 0, ((SoftwareEvent) event).getSubtype(),
						((SoftwareEvent) event).getMessage(), "SOFTWARE");
				new Filter(standard, S_policies, ds);
			} else if (event.getType() == EventType.HARDWARE) {
				standard = new Standard(timeStamp,  event.getType(),
						((HardwareEvent) event).getState(),
						((HardwareEvent) event).getPosition(),
						((HardwareEvent) event).getTemperature(), null, null,
						"HARDWARE");
				new Filter(standard, H_policies, ds);
			}
		} else {
			return;
		}
	}

}
